#include <gtk/gtk.h>

static GtkWidget *draw_area = NULL;
static void on_draw(
    GtkDrawingArea *area, 
    cairo_t *cr, 
    int width, 
    int height, 
    gpointer data
) {
    printf("Calling %s\n", __func__);
    // 清空背景
    cairo_set_source_rgb(cr, 0.1, 0.1, 0.1);
    cairo_paint(cr);

    // 绘制红色圆形
    cairo_set_source_rgb(cr, 1, 0, 0);
    cairo_arc(cr, width/2, height/2, MIN(width, height)/3, 0, 2 * M_PI);
    cairo_fill(cr);

    // 绘制文字
    cairo_set_source_rgb(cr, 1, 1, 1);
    PangoLayout *layout = pango_cairo_create_layout(cr);
    pango_layout_set_text(layout, "GTK4 Cairo Drawing", -1);
    cairo_move_to(cr, 20, 20);
    pango_cairo_show_layout(cr, layout);
    g_object_unref(layout);
}

static void on_surface_layout(GdkSurface *surface, 
                             int width, 
                             int height, 
                             gpointer data) 
{

    g_print("窗口Size (%d,%d)\n",
                    width, height);
}


static gboolean on_gdk_event(GdkSurface *self, GdkEvent *event, gpointer data)
{
    g_print("Gdk Event type: %d\n", gdk_event_get_event_type(event));
    return FALSE;
}

static gboolean idle_callback(gpointer data)
{
    static cairo_region_t *region = NULL;
    GdkSurface *surface = gtk_native_get_surface(gtk_widget_get_native(GTK_WIDGET(data)));
    gboolean ret;
    GdkRectangle rect = {0, 0, 1920, 1080};
    if(region == NULL)
    {
        region = cairo_region_create_rectangle(&rect);
    }
    gtk_widget_queue_draw(draw_area);
    g_signal_emit_by_name(surface, "render", region, &ret);
    //return G_SOURCE_REMOVE;
    return G_SOURCE_CONTINUE;
}
static void on_activate(GtkApplication *app, gpointer data)
{
}

static void on_realize(GtkWidget *window, gpointer data)
{
    //idle_callback(window);
    GdkSurface *surface = gtk_native_get_surface(GTK_NATIVE(window));
    g_signal_connect(surface, "layout", G_CALLBACK(on_surface_layout), window);
    g_signal_connect(surface, "event", G_CALLBACK(on_gdk_event), window);
}


int main(int argc, char **argv)
{
    //GtkApplication *app = gtk_application_new("com.example.draw", G_APPLICATION_DEFAULT_FLAGS);
    gtk_init();   

    //GtkWidget *window = gtk_application_window_new(app);
    GtkWidget *window = gtk_window_new();
    draw_area = gtk_drawing_area_new();
    
    // 设置绘图函数
    gtk_drawing_area_set_draw_func(
        GTK_DRAWING_AREA(draw_area),
        on_draw,
        NULL,
        NULL
    );
    
    // 设置初始尺寸
    gtk_drawing_area_set_content_width(GTK_DRAWING_AREA(draw_area), 400);
    gtk_drawing_area_set_content_height(GTK_DRAWING_AREA(draw_area), 300);
    
    gtk_window_set_child(GTK_WINDOW(window), draw_area);


    //gtk_widget_realize(window);
    //idle_callback(window);
    g_signal_connect(window, "realize", G_CALLBACK(on_realize), NULL);

    gtk_window_set_decorated(GTK_WINDOW(window), FALSE);
    gtk_widget_set_focusable(window, FALSE);
    gtk_widget_set_can_focus(window, FALSE);

    g_idle_add(idle_callback, window);

    //g_signal_connect(app, "activate", G_CALLBACK(on_activate), window);
    gtk_window_present(GTK_WINDOW(window));


    //return g_application_run(G_APPLICATION(app), argc, argv);
    g_main_loop_run(g_main_loop_new(NULL, FALSE));
    return 0;
    
}
